package com.lingxu.module.redList.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lingxu.base.common.api.vo.Result;
import com.lingxu.base.common.constant.LogType;
import com.lingxu.base.config.LogInfoHandler;
import com.lingxu.module.message.entity.UserMessage;
import com.lingxu.module.message.mapper.MessageMapper;
import com.lingxu.module.redList.entity.IdCard;
import com.lingxu.module.redList.entity.RedListTemp;
import com.lingxu.module.redList.entity.TempApproval;
import com.lingxu.module.redList.mapper.RedListTempMapper;
import com.lingxu.module.redList.service.RedListTempService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @Title: RedListTempServiceImpl
 * @Package: com.lingxu.module.redList.service.impl
 * @Author: ysj闫世杰
 * @Email: 1105324813@qq.com
 * @Date: Created in 2023/6/25 16:56
 * @Description:
 * @Version: 1.0.0
 */
@Slf4j
@Service
public class RedListTempServiceImpl extends ServiceImpl<RedListTempMapper, RedListTemp>
        implements RedListTempService {

    @Autowired
    private RedListTempMapper redListTempMapper;
    @Autowired
    private MessageMapper messageMapper;

    @Override
    public Result findRedListTemp(Integer page, Integer limit, String status) {
        try {
            Page<RedListTemp> listPage = new Page<>(page,limit);
            IPage<RedListTemp> redListTemp = redListTempMapper.findNeedApprovalList(listPage,status);
            return Result.ok(redListTemp,"查询成功");
        }catch (Exception e){
            log.error("数据查询异常",e);
            return Result.error("查询异常");
        }
    }

    @Override
    public int findOnly(String idcard) {
        return redListTempMapper.findOnly(idcard);
    }


    @Override
    public Result updateApprovalBatch(TempApproval approval) {
        try {
            String content = "";
            List<IdCard> idCardList = approval.getIdCardList();
            List<String> ids = idCardList.stream().map(IdCard::getId).collect(Collectors.toList());
            approval.setIds(ids);
            //批量审批
            int i = redListTempMapper.updateApprovalBatch(approval);
            if(i > 0){

                //同意，直接执行比对更新记录表
                if("1".equals(approval.getStatus())){
                    for(IdCard card : idCardList){
                        //按照身份证号码查询是否是红名单人员
                        int row = redListTempMapper.checkRedList(card.getIdcard());
                        RedListTemp temp = new RedListTemp();
                        temp.setId(card.getId());
                        //是
                        if(row > 0){
                            temp.setIsRed("1");
                        }else {
                            //否
                            temp.setIsRed("0");
                        }
                        redListTempMapper.updateById(temp);
                    }
                    //同意消息
                    content = "您发起的红名单比对申请已通过。";
                }else {
                    //拒绝消息
                    content = "您发起的红名单比对申请未通过。";
                }
                //按照userId去重复，消息提醒一次就可以
                Set<String> userIds = idCardList.stream().map(IdCard::getCreateUser).collect(Collectors.toSet());
                for(String s : userIds){
                    UserMessage message = new UserMessage();
                    message.setUserId(s);
                    message.setIsRead("0");
                    message.setContent(content);
                    messageMapper.insert(message);
                }

                String nr = "在【"+ new SimpleDateFormat("yyyy-MM-dd").format(new Date()) +
                        "】审批红名单比对申请记录" + approval.getIdCardList().size() + "条。";
                new LogInfoHandler().setLogInfoHandler(LogType.LOG_RED_LIST_CHECK,nr);
                return Result.ok("操作成功");
            }
            return Result.error("操作失败");
        }catch (Exception e){
            log.error("操作异常",e);
            return Result.error("操作异常");
        }
    }
}
